<?php
namespace Admin\Controller;
use Think\Controller;
use Admin\Model\AdminUserModel;

class LoginController extends Controller{
    public function index ()
    {
        if(!empty($_SESSION['admin'])){
            $this->error("已经登录",U("Index/index"));
        }
        $this->assign("title","管理员后台");
        $this->display("Login/index");
    }

    //后台管理员登录
    public final function login ()
    {
        //判断表单是否为空
        foreach(I("post.") as $value){
            if(empty($value)){
                $this->errot("账号密码不能为空",U("Login/index"));
            }
        }
        // 获取表单账号密码
        $data['name'] = I("post.name");
        $data['password'] = md5(I("post.pwd"));

        //实例化后台管理员模型
        $user = new AdminUserModel();
        $res = $user->login($data);

        if($res > 0){
            $this->success("登录成功",U('Index/index'));
        }else{
            $this->error("账号或密码不正确");
        }


    }

    //后台管理员退出
    public function loginout ()
    {
        $_SESSION['admin'] = null;
        $this->success("已退出登录",U('Login/index'));
    }
}
?>